package pri.black.dogview.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.spring6.context.SpringContextUtils;
import pri.black.dogview.domain.DeviceGpsLog;
import pri.black.dogview.domain.DeviceLogs;
import pri.black.dogview.domain.vo.SplitData;
import pri.black.dogview.mapper.DeviceLogsMapper;
import pri.black.dogview.service.IDeviceGpsLogService;
import pri.black.dogview.service.IDeviceLogsService;
import pri.black.dogview.util.StringUtils;

import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

@Service
public class DeviceLogsServiceImpl extends ServiceImpl<DeviceLogsMapper, DeviceLogs> implements IDeviceLogsService {

    @Autowired
    private IDeviceGpsLogService deviceGpsLogService;

    @Override
    public List<SplitData> queryTraceData() {
        List<DeviceLogs> deviceLogs = getBaseMapper().splitData();
        List<SplitData> datas = new LinkedList<>();
        List<LocalDateTime> createTimes = new java.util.ArrayList<>(deviceLogs.stream().filter(Objects::nonNull).map(DeviceLogs::getCreateTime).toList());
        createTimes.sort(
                new Comparator<LocalDateTime>() {
                    @Override
                    public int compare(LocalDateTime o1, LocalDateTime o2) {
                        return o1.isBefore(o2) ? 1: 0;
                    }
                }
        );
        int len = createTimes.size();
        LocalDateTime last = null;
//        for (int i = 1; i <= len; i++) {
//            SplitData splitData = new SplitData();
//            splitData.setStartTime(createTimes.get(i - 1));
//            if (i <= len - 1) {
//                splitData.setEndTime(createTimes.get(i));
//            }
//            splitData.setIndex(i);
//            datas.add(splitData);
//        }
        for (int i = len; i > 0; i--) {
            SplitData splitData = new SplitData();
            splitData.setStartTime(createTimes.get(i - 1));
            if (i <= len - 1) {
                splitData.setEndTime(createTimes.get(i));
            }
            splitData.setIndex(i);
            datas.add(splitData);
        }
        return datas;
    }

    @Override
    public List<DeviceGpsLog> traceDuration(String start, String end) {
        List<DeviceLogs> list = this.lambdaQuery()
                .like(DeviceLogs::getData,"2{%")
                .ge(StringUtils.isNotEmpty(start),DeviceLogs::getCreateTime, start)
                .lt(StringUtils.isNotEmpty(end),DeviceLogs::getCreateTime, end)
                .list();
        List<DeviceGpsLog> data = list.stream()
                .map(DeviceLogs::getData)
                .map(e -> e.substring(1))
                .map(e -> JSONObject.parseObject(e).getJSONObject("GeoLocation").toJavaObject(DeviceGpsLog.class)).toList();
        return data;
    }
}
